package com.enki.cigarettes.controller;

import com.enki.cigarettes.controller.vo.response.InvitationResponseVo;
import com.enki.cigarettes.entity.OrderT;
import com.enki.cigarettes.entity.UserT;
import com.enki.cigarettes.enums.OrderEnums;
import com.enki.cigarettes.handler.Response;
import com.enki.cigarettes.repository.OrderTRepository;
import com.enki.cigarettes.repository.UserTRepository;
import com.enki.cigarettes.statics.StaticKeys;
import com.enki.cigarettes.utils.DateUtils;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author WCS.Wang
 * @version V1.0
 * @Package com.enki.cigarettes.controller
 * @Name e-cigarettes-shop-api-module
 * @Description: TODO 邀请
 * @date 2019-08-14
 */
@RestController
@RequestMapping("/api/invitation")
@Slf4j
public class InvitationController {

    @Autowired
    UserTRepository userTRepository;
    @Autowired
    OrderTRepository orderTRepository;

    @ApiOperation(value = "获取邀请记录")
    @GetMapping(value = "/record")
    public Object findInvitationRecord(HttpSession session) {
        try {
            Object sessionId = session.getAttribute(StaticKeys.USER_ID);
            if (sessionId == null) {
                return Response.unauthorized_401("登录身份失效，请重新登录");
            }
            UserT userBean = userTRepository.findOneById(sessionId.toString());
            if (userBean == null) {
                return Response.unauthorized_401("登录身份失效，请重新登录");
            }
            List<InvitationResponseVo> invitationItems = new ArrayList<>();
            List<UserT> beanItems = userTRepository.findByInviteeCode(userBean.getInvitationCode());
            for (UserT bean : beanItems) {
                InvitationResponseVo invitationItem = new InvitationResponseVo();
                invitationItem.setNickname(bean.getNickname());
                invitationItem.setCreateTime(DateUtils.getDateYMDHMS(bean.getCreateTime()));
                OrderT firstOrder = orderTRepository.findFirstByUserTIdAndFirstOrder(bean.getId(), true);
                if (firstOrder != null) {
                    if (firstOrder.getOrderStatus() == OrderEnums.OrderStatus.SIGNING.getStatus()) {
                        invitationItem.setStatusName("邀请成功");
                    } else {
                        invitationItem.setStatusName("未签收");
                    }
                } else {
                    invitationItem.setStatusName("未下单");
                }
                invitationItems.add(invitationItem);
            }
            return Response.success(invitationItems, StaticKeys.SUCCESS);
        } catch (Exception e) {
            log.error("获取邀请记录失败：" + e.getMessage());
            e.printStackTrace();
            return Response.serverError_500("获取邀请记录失败");
        }
    }

}
